
$.fn.scrollStopped = function(callback) {          
    $(this).scroll(function(){
        var self = this, $this = $(self);
        if ($this.data('scrollTimeout')) {
          clearTimeout($this.data('scrollTimeout'));
        }
        $this.data('scrollTimeout', setTimeout(callback,250,self));
    });
};

$(document).ready(function() {
	
	$(".post").addClass("collapsed");
	$(".expandable").hide();
	
	$(".expander").click(function(e){
		e.preventDefault();
		
		if($(this).hasClass("expander-down")){
			$(this).parent().find(".expandable").slideDown();
			$(this).removeClass("expander-down");
			$(this).addClass("expander-up");
			$(this).parent().removeClass("collapsed")
		}
		else if($(this).hasClass("expander-up")){
			$(this).parent().find(".expandable").slideUp();
			$(this).addClass("expander-down");
			$(this).removeClass("expander-up");
			$(this).parent().addClass("collapsed")
		}
	});
	
	$(window).scrollStopped(function(){
		var top = $(window).scrollTop();
		var topThresh = 575;
		var bottomThresh = 1170;
		var bottomPos = 615;
		
		if(top < topThresh){
			$("#sidebar").animate({top: 0 + "px"}, 500);
		}
		else if(top > topThresh && top < bottomThresh){
			$("#sidebar").animate({top: ($(window).scrollTop() - (topThresh - 20)) + "px"}, 500);
		}
		else if(top > bottomThresh){
			$("#sidebar").animate({top: bottomPos + "px"}, 500);
		}
	});
	
});